from entity.mistake_details import Mistake_details
from database.database import Database

class OUTPUT:

    def __init__(self,uid):
        self.uid = uid

    def output_users_highest_mistake_details(self):

        db = Database()

        results = db.get_all_mistake_details_by_uid(self.uid)
        print(results)

        # 将查询结果存储为对象列表
        objects_list = []
        for result in results:
            obj = Mistake_details(result[0], result[1], result[2], result[3], result[4], result[5],result[6])
            objects_list.append(obj)

        # 计算总体 weight
        total_weight = sum(obj.weight for obj in objects_list)

        # 计算每个对象 weight 在总体 weight 中的占比
        for obj in objects_list:
            obj.weight_percentage = (obj.weight / total_weight) * 100

        # 找出占比最高的两个对象的 type 和 details 属性
        top_objects = sorted(objects_list, key=lambda x: x.weight_percentage, reverse=True)[:2]

        # 输出占比最高的两个对象的 type 和 details 属性
        print("The Highest Weight's Mistake Details for "+self.uid+" are")
        for obj in top_objects:
            print(f"{obj.type}  {obj.details}")

        db.close_connection()